/**
 * Created by hxpeng on 2017/3/24.
 */
(function () {
    'use strict';

    angular.module('App.pages.member.entrust')
        .controller('TeacherEntrustCtrl', TeacherEntrustCtrl);


    function TeacherEntrustCtrl($scope,$state,AppUtils,EntrustService,$timeout,$interval,$sce,LoginAccountAuth,$ionicScrollDelegate) {

        var vm = this;
        vm.entrustTitle = "嘱托列表";
        vm.isUnsolved = true;
        vm.unsolvedExhortaions = [];
        vm.solvedExhortaions = [];
        vm.unsolvedPageIndex = 0;
        vm.solvedPageIndex = 0;
        vm.exhortaions = [];
        vm.loadUnsolvedExhortaionsFlag = 'loading';//neverLoad|loadFinish|loading
        vm.loadSolvedExhortaionsFlag = 'neverLoad';
        vm.loadMoreUnsolvedBtnStatus = true;
        vm.loadMoreSolvedStatus = true;
        vm.loadMoreBtnStr = "加载更多";
        vm.entrustStatusParams = 'unexecuted';//unexecuted待办嘱托状态,executed为已办嘱托状态
        vm.sortType = 'entrustTime';
        vm.pageIndex = 1;//第一页

        //切换不同嘱托状态列表
        $scope.changeStatus = function(status) {
            vm.exhortaions = [];
            if($scope.isUnsolved != status){
                if(status == true){
                    vm.isUnsolved = true;
                    vm.entrustTitle = "待办列表";
                    vm.entrustStatusParams = 'unexecuted';
                    vm.sortType = 'entrustTime';
                    if(vm.loadUnsolvedExhortaionsFlag == 'neverLoad'){
                        vm.pageIndex = 1;//第一页
                        $scope.loadMoreEntrust();
                        console.log(111111111111111);
                    }else if(vm.loadUnsolvedExhortaionsFlag == 'loading'){
                        vm.loadMoreUnsolvedBtnStatus = true;
                    }else if(vm.loadUnsolvedExhortaionsFlag == 'loadFinish'){
                        vm.loadMoreUnsolvedBtnStatus = false;
                    }
                }else if(status == false){
                    vm.isUnsolved = false;
                    vm.entrustTitle = "已办列表";
                    vm.entrustStatusParams = 'execute';
                    vm.sortType = 'executeTime';
                    if(vm.loadSolvedExhortaionsFlag == 'neverLoad'){
                        vm.pageIndex = 1;//第一页
                        $scope.loadMoreEntrust();
                    }else if(vm.loadSolvedExhortaionsFlag == 'loading'){
                        vm.loadMoreSolvedStatus = true;
                        vm.loadMoreBtnStr = "加载更多";
                    }else if(vm.loadSolvedExhortaionsFlag == 'loadFinish'){
                        vm.loadMoreSolvedStatus = false;
                        vm.loadMoreBtnStr = "没有更多数据了";
                    }
                }
                //AppUtils.scrollTop();
            }
        }


        LoginAccountAuth.getAccount()
            .then(function(data){
                vm.loginAccount = data;
                vm.getEntrustPage();
            });

        //往上拉加载更多
        $scope.loadMoreEntrust = function(){
            if(vm.entrustStatusParams == 'unexecuted' && vm.loadMoreUnsolvedBtnStatus == true){
                vm.pageIndex = vm.unsolvedPageIndex + 1;
                vm.getEntrustPage();
            }else if(vm.entrustStatusParams == 'execute' && vm.loadMoreSolvedStatus == true){
                vm.pageIndex = vm.solvedPageIndex + 1;
                vm.getEntrustPage();
            }
        }


        vm.getEntrustPage = function(){
            var params = {};
            params.entrustState = vm.entrustStatusParams;
            params.pageInfo = {};
            params.pageInfo.pageSize = 10;
            params.pageInfo.pageNumber = vm.pageIndex;

            EntrustService.loadEntrustPage(params).then(function(response){
                if(response.stat == 'err'){
                    AppUtils.showAlert(response.msg);
                }else{
                    var reponseDate = response.pageResult;
                    var items = reponseDate.pageResult;
                    var pageIndex = reponseDate.pageIndex;
                    var pageTotalPageSize = reponseDate.pageTotalPageSize;
                    if(pageIndex == pageTotalPageSize || items.length == 0){
                        vm.loadMoreBtnStr = "没有更多数据了";
                        if(vm.entrustStatusParams == 'unexecuted'){
                            vm.loadUnsolvedExhortaionsFlag = 'loadFinish';//不再加载待办嘱托列表数据
                            vm.loadMoreUnsolvedBtnStatus = false;
                        }else if(vm.entrustStatusParams == 'execute'){
                            vm.loadSolvedExhortaionsFlag = 'loadFinish';//不再加载已办嘱托列表数据
                            vm.loadMoreSolvedStatus = false;
                        }
                    }
                    if(vm.entrustStatusParams == 'unexecuted'){
                        vm.unsolvedPageIndex = pageIndex;
                        angular.forEach(items,function(data,index,array){
                            vm.unsolvedExhortaions.push(data);
                        })
                    }else{
                        if(vm.loadSolvedExhortaionsFlag != 'loadFinish'){
                            vm.loadSolvedExhortaionsFlag = 'loading';
                        }
                        vm.solvedPageIndex = pageIndex;
                        angular.forEach(items,function(data,index,array){
                            vm.solvedExhortaions.push(data);
                        })
                    }
                }
            }).finally(function() {
                $scope.$broadcast('scroll.infiniteScrollComplete');
            });
        }
    }

})();
